//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.UIDesigner.Monitor
{
    using System.Collections.ObjectModel;
    using System.Collections.Specialized;

    public class ActivityViewModel 
    {
        private string label;

        /// <summary>
        /// Initializes a new instance of the activity view model for the input/output tab in the monitor.
        /// </summary>
        public ActivityViewModel()
        {
            this.DisplayItems = new ObservableCollection<object>();
            this.InputProperties = new ObservableCollection<ActivityProperty>();
            this.OutputProperties = new ObservableCollection<ActivityProperty>();
            this.Properties = new ObservableCollection<ActivityPropertyModel>();
            this.Properties.CollectionChanged += new NotifyCollectionChangedEventHandler(CollectionChanged);
            this.Properties.Add(new ActivityPropertyModel { InputProperties = this.InputProperties, OutputProperties = this.OutputProperties });
            this.Iterations = new ObservableCollection<InputOutputIterationViewModel>();
            this.Iterations.CollectionChanged += new NotifyCollectionChangedEventHandler(CollectionChanged);
        }

        /// <summary>
        /// Gets the input properties of the activity.
        /// </summary>
        public ObservableCollection<ActivityProperty> InputProperties { get; private set; }

        /// <summary>
        /// Gets the output properties of the activity. 
        /// </summary>
        public ObservableCollection<ActivityProperty> OutputProperties { get; private set; }

        /// <summary>
        /// Gets the properties of the activity.
        /// </summary>
        public ObservableCollection<ActivityPropertyModel> Properties { get; private set; }

        /// <summary>
        /// Gets the items to be shown to the user. Includes properties and iterations.
        /// </summary>
        public ObservableCollection<object> DisplayItems { get; private set; }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets the Iterations of the activity.
        /// </summary>
        public ObservableCollection<InputOutputIterationViewModel> Iterations { get; private set; }
        
        /// <summary>
        /// Gets or sets the label.
        /// </summary>
        public string Label 
        {
            get
            {
                if (string.IsNullOrEmpty(this.label))
                {
                    return Name;
                }
                return this.label;
            }
            set
            {
                this.label = value;
            }
        }

        /// <summary>
        /// Gets the current iteration.
        /// </summary>
        public InputOutputIterationViewModel CurrentIteration
        {
            get
            {
                return Iterations.Count > 0 ? Iterations[Iterations.Count - 1] : null;
            }
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override bool Equals(object obj)
        {
            ActivityViewModel far = obj as ActivityViewModel;
            
            return (far != null && far.Name == this.Name) ? true : false;
        }

        /// <summary>
        /// Event handler for the collection changed event on the Property and the iteration collections.
        /// </summary>
        /// <param name="sender">Origin of the event.</param>
        /// <param name="e">The event handler.</param>
        private void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems.Count > 0)
            {
                if (e.NewItems[0] is ActivityPropertyModel)
                {
                    this.DisplayItems.Insert(e.NewStartingIndex, e.NewItems[0]);
                }
                else if (e.NewItems[0] is InputOutputIterationViewModel)
                {
                    int offset = this.Properties.Count;
                    this.DisplayItems.Insert(e.NewStartingIndex + offset, e.NewItems[0]);
                }
            }
            else if (e.OldItems.Count > 0)
            {
                this.DisplayItems.Remove(e.OldItems[0]);
            }
        }
    }
}
        